package com.example.ble_20220504;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

/***
 * 设备列表适配器
 */
public class ListAdpter extends ArrayAdapter<BleDevice> {

    private int resourceId;
    public ListAdpter(@NonNull Context context, int resource, @NonNull List<BleDevice> objects) {
        super(context, resource, objects);
        resourceId = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        BleDevice bleDevice = getItem(position);
        View view;
        if (convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
        }else {
            view = convertView;
        }

        TextView ble_Name = (TextView) view.findViewById(R.id.ble_name);
        TextView ble_Address = (TextView) view.findViewById(R.id.ble_address);
        TextView ble_Rssi = (TextView) view.findViewById(R.id.ble_rssi);

        ble_Name.setText(bleDevice.getBle_name());
        ble_Address.setText(bleDevice.getBle_address());
        ble_Rssi.setText(bleDevice.getRssi());

        return view;
    }
}
